<template>
  <div class="login_con">
    <el-card class="box-card">
      <img src="../../assets/img/cn.png" alt="" class="img3" />
      <img src="../../assets/img/logo.png" alt="" class="img1" />

      <!-- 登录表单 -->
      <el-form class="form_con" :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item prop="account">
          <div style="font-size: 16px; font-weight: 600">
            {{ $t("lang.account") }}:
          </div>
          <el-input class="input-width" v-model="ruleForm.account"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <div style="font-size: 16px; font-weight: 600">
            {{ $t("lang.password") }}:
          </div>
          <el-input type="password" v-model="ruleForm.password"></el-input>
          <el-link
            href="https://element.eleme.io"
            target="_blank"
            style="font-size: 14px; font-weight: 400"
            >忘记密码？</el-link
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="input-width"
            @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>

        <el-form-item>
          <el-select
            v-model="lang"
            :placeholder="$t('lang.lang')"
            @change="changeLang"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "loginView",
  data() {
    return {
      ruleForm: {
        account: "", //用户名
        password: "", //密码
      },
      rules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      options: [
        { value: "zh", label: this.$t("lang.cs") },
        { value: "en", label: this.$t("lang.is") },
      ],
      lang: "",
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // alert("submit!");
          //登录操作写到vuex里面,调用 vuex中的action
          this.$store.dispatch("LoginAction", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    changeLang() {
      this.$store.commit("SET_LANG", this.lang);
      this.$router.go(0);
    },
  },
};
</script>

<style lang="scss" scoped>
.login_con {
  width: 100%;
  height: 100vh;
  background: #ebeef2;
  position: relative;
  .box-card {
    position: absolute;
    width: 380px;
    height: 500px;
    left: calc(50% - 190px);
    top: 140px;
    .img3 {
      position: absolute;
      top: 0;
      right: 0;
      width: 70px;
    }
    .img1 {
      position: absolute;
      top: 40px;
      right: calc(50% - 80px);
      width: 160px;
    }
    .form_con {
      position: absolute;
      top: 140px;
      left: 40px;
    }
  }
}
.input-width {
  width: 300px;
}
</style>